#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.${parentArtifactId}.modules.sys.converter;

import ${package}.${parentArtifactId}.libcommon.util.TreeUtil;
import org.apache.commons.collections.CollectionUtils;
import ${package}.${parentArtifactId}.common.converter.BaseConverter;
import ${package}.${parentArtifactId}.modules.sys.entity.SysOrg;
import ${package}.${parentArtifactId}.modules.sys.vo.SysOrgVo;
import ${package}.${parentArtifactId}.modules.sys.dto.SysOrgDTO;
import ${package}.${parentArtifactId}.modules.sys.vo.SysOrgTreeVo;
import ${package}.${parentArtifactId}.modules.sys.vo.TreeSelectVo;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SysOrg转换器
 * @Description Entity、VO、DTO相互转换器
 * @Author Created by xudp (alias:孤傲苍狼) 290603672@qq.com
 * @Date 2020-10-15
 * @Version 1.0
 */
public class SysOrgConverter extends BaseConverter<SysOrg, SysOrgVo, SysOrgDTO> {

    public static SysOrgConverter build() {
        return new SysOrgConverter();
    }

    @Override
    public SysOrgVo vo(SysOrg entity) {
        SysOrgVo sysOrgVo = new SysOrgVo();
        BeanUtils.copyProperties(entity,sysOrgVo);
        return sysOrgVo;
    }

    @Override
    public SysOrg entity(SysOrgDTO entityDto) {
        SysOrg sysOrg = new SysOrg();
        BeanUtils.copyProperties(entityDto,sysOrg);
        return sysOrg;
    }

    public SysOrgTreeVo treeVo(SysOrg entity){
        SysOrgTreeVo treeVo = new SysOrgTreeVo();
        BeanUtils.copyProperties(entity,treeVo);
        treeVo.setId(entity.getId());
        if(entity.getParentId() != null){
            treeVo.setParentId(entity.getParentId());
        }
        return treeVo;
    }

    public List<SysOrgTreeVo> treeListVo(List<SysOrg> sysOrgList){
        List<SysOrgTreeVo> treeVoList = new ArrayList<>();
        sysOrgList.forEach(sysOrg -> treeVoList.add(treeVo(sysOrg)));
        return treeVoList;
    }

    public TreeSelectVo treeSelectVo(SysOrgTreeVo treeVo) {
        TreeSelectVo treeSelectVo = new TreeSelectVo();
        treeSelectVo.setId(treeVo.getId());
        treeSelectVo.setLabel(treeVo.getName());
        findChildren(treeSelectVo,treeVo);
        return treeSelectVo;
    }

    public List<TreeSelectVo> treeSelectListVo(List<SysOrgTreeVo> sysOrgTreeVoList) {
        List<SysOrgTreeVo> treeVoList = TreeUtil.buildTree(sysOrgTreeVoList);
        List<TreeSelectVo> treeSelectVos = new ArrayList<>();
        treeVoList.forEach(treeVo -> {
            TreeSelectVo treeSelectVo = treeSelectVo(treeVo);
            treeSelectVos.add(treeSelectVo);
        });
        return treeSelectVos;
    }

    /**
    * 递归子节点
    */
    private void findChildren(TreeSelectVo treeSelectVo,SysOrgTreeVo treeNodeVo){
        List<TreeSelectVo> childrenVoList = new ArrayList<>();
        //子节点信息
        List<SysOrgTreeVo> subNodeList = treeNodeVo.getChildren();
        if(CollectionUtils.isNotEmpty(subNodeList)){
            //子节点信息不为空
            for(SysOrgTreeVo treeVo:subNodeList) {
                TreeSelectVo selectVo = new TreeSelectVo();
                selectVo.setId(treeVo.getId());
                selectVo.setLabel(treeVo.getName());
                childrenVoList.add(selectVo);
                findChildren(selectVo,treeVo);//递归
            }
        }
        treeSelectVo.setChildren(childrenVoList);
    }

}
